//----------------------------------------------------
// T7Game - Copyright (c) 湖南淘气网络科技有限公司
// Author:
// Date:2017/6/6
// Description:
//----------------------------------------------------
using System.IO;
using UnityEngine;
using UnityEngine.Rendering;

namespace T7Game
{
    public enum DeviceRank
    {
        Low,
        Mid,
        High
    }
    [XLua.Hotfix]
    class UPlatform : Singleton<UPlatform>
    {
        private string m_zipPackPath;
        private string m_assetPackPath;
        private string m_gameResPackPath;
        public string WriteablePath
        {
            get
            {
                if(Application.isMobilePlatform)
                {
                    return Application.persistentDataPath;
                }
                else if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
                {
                    string dir = Path.GetDirectoryName(Application.dataPath + "/../WriteblePath/");
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    return dir;
                }
                else
                {
                    return Application.dataPath;
                }
            }
        }

        private string GetAssetBundleBaseUrl()
        {
            return Application.streamingAssetsPath + "/";
        }
        

        public string GetCurPackPlatformStr()
        {
#if UNITY_IOS
            return "ios-";
#else
            return "android-";
#endif

        }
        public string GetZipPackPath()
        {
            if (string.IsNullOrEmpty(m_zipPackPath))
            {
                m_zipPackPath = WriteablePath + "/patch/";
            }

            return m_zipPackPath; 
        }
        public string GetAssetPackPath()
        {
            if (string.IsNullOrEmpty(m_assetPackPath))
            {
                m_assetPackPath = WriteablePath + "/packages/";
            }

            return m_assetPackPath;
        }

        public string GetGameResPackPath()
        {
            if (string.IsNullOrEmpty(m_gameResPackPath))
            {

                m_gameResPackPath = WriteablePath + "/gameres/";
            }

            return m_gameResPackPath;
        }

        public string GetDownloadingURL(string assetDir, bool isToLower = true)
        {
            string assetLowerDir = assetDir;
            if (isToLower)
            {
                assetLowerDir = assetDir.ToLower();
            }
            string packPath = GetAssetPackPath() + assetLowerDir;
            if (File.Exists(packPath))
            {
                return packPath;
            }

            string gameResPath = GetGameResPackPath() + assetLowerDir;
            if (File.Exists(gameResPath))
            {
                return gameResPath;
            }

            return GetAssetBundleBaseUrl() + assetLowerDir;
        }
        public static bool IsSupportDevice()
        {
#if DEVELOPMENT_BUILD || UNITY_EDITOR
            Debug.Log(GetDeviceBasicInfo());
#endif
            var isSupport = false;
            var deviceType = SystemInfo.graphicsDeviceType;
            if (deviceType == GraphicsDeviceType.Direct3D11 ||
                deviceType == GraphicsDeviceType.Direct3D12 ||
                deviceType == GraphicsDeviceType.OpenGLCore||
                deviceType == GraphicsDeviceType.OpenGLES3 ||
                deviceType == GraphicsDeviceType.OpenGLES2 ||
                deviceType == GraphicsDeviceType.Vulkan ||
                deviceType == GraphicsDeviceType.Metal)
            {
                isSupport = true;
            }
            var cpuMem = SystemInfo.systemMemorySize;
            if(cpuMem > 768 || cpuMem == 0)
            {
                isSupport = isSupport && true;
            }

            return isSupport;
        }

        public static DeviceRank GetDeviceRank()
        {
            int supportsInstancingScore = 50;
            var supportsInstancing = SystemInfo.supportsInstancing;
            if(supportsInstancing)
            {
                supportsInstancingScore = 100;
            }
            var gpuMem = SystemInfo.graphicsMemorySize;
            var gpuMemScore = 50;
            if(gpuMem >= 512)
            {
                gpuMemScore = 100;
            }
            var cpuMem = SystemInfo.systemMemorySize;
            var cpuMemScore = 50;
            if(cpuMem > 1024)
            {
                cpuMemScore = 100;
            }
            var cpuCoreCount = SystemInfo.processorCount;
            var cpuCoreCountSore = 50;
            if(cpuCoreCount > 4)
            {
                cpuMemScore = 100;
            }

            var rank = (supportsInstancingScore + gpuMemScore + cpuMemScore + cpuCoreCountSore) / 4;

            if (rank == 100)
            {
                return DeviceRank.High;
            }
            else if(rank >= 70)
            {
                return DeviceRank.Mid;
            }
            else
            {
                return DeviceRank.Low;
            }
        }

        private static string GetDeviceBasicInfo()
        {
            var gpuName = SystemInfo.graphicsDeviceName;
            var gpuMem = SystemInfo.graphicsMemorySize;
            var gpuSupportInstancing = SystemInfo.supportsInstancing;
            var gpuApi = SystemInfo.graphicsDeviceType;
            var gpuDeviceVersion = SystemInfo.graphicsDeviceVersion;

            var processorType = SystemInfo.processorType;
            var deviceModel = SystemInfo.deviceModel;
            var cpuMem = SystemInfo.systemMemorySize;
            var cpuFrequency = SystemInfo.processorFrequency;
            var cpuCoreCount = SystemInfo.processorCount;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("gpuName:").Append(gpuName).Append("\n");
            sb.Append("gpuMem:").Append(gpuMem).Append("\n");
            sb.Append("gpuSupportIntancing:").Append(gpuSupportInstancing).Append("\n");
            sb.Append("gpuApi:").Append(gpuApi).Append("\n");
            sb.Append("gpuDeviceVersion:").Append(gpuDeviceVersion).Append("\n");

            sb.Append("deviceModel:").Append(deviceModel).Append("\n");
            sb.Append("processorType:").Append(processorType).Append("\n");
            sb.Append("cpuMem:").Append(cpuMem).Append("\n");
            sb.Append("cpuFrequency:").Append(cpuFrequency).Append("\n");
            sb.Append("cpuCoreCount:").Append(cpuCoreCount).Append("\n");

            return sb.ToString();
        }
    }
}